package learn.advance.reflect1;

import com.sun.tools.doclets.standard.Standard;
import com.sun.tools.javac.code.Attribute;
import com.sun.xml.internal.xsom.impl.Const;
import learn.advance.reflect1.domain.Person;

import javax.crypto.spec.PSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Properties;

class  Demo1Reflect{
    public static void Test() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
        Class cls1=Class.forName("learn.advance.reflect1.domain.Person");
        System.out.println(cls1);
        Class cls2= Person.class;
        System.out.println(cls2);
        Person p=new Person();
        Class cls3=p.getClass();
        System.out.println(cls3);

        System.out.println(cls1==cls2);
        System.out.println(cls2==cls3);

        System.out.println("======================");
        Field[] fields=cls1.getFields();
        for(Field f:fields){
            System.out.println(f);
        }

        Person p2=new Person("小明",20,"哈哈");
        Field a=cls1.getDeclaredField("a_");

        Object value=a.get(p2);
        System.out.println(value);

        a.set(p2,"呵呵");
        System.out.println(p2.a_);

        System.out.println("======================");
        Field[] declared_field = cls1.getDeclaredFields();
        for (Field f:declared_field){
            System.out.println(f);
        }
        System.out.println("=====================");

        Field age=cls1.getDeclaredField("age_");
        age.setAccessible(true);//暴力反射

        Object value2=age.get(p2);
        System.out.println(value2);

        System.out.println("===========================");
        Constructor constructor=cls1.getConstructor();
        System.out.println(constructor);

        Constructor constructor1=cls1.getConstructor(String.class,int.class,String.class);
        System.out.println(constructor1);
        Object person=constructor1.newInstance("小明",12,"hhh");
        System.out.println(((Person)person).a_);
        System.out.println("===========================");

        Method func=cls1.getMethod("SetName",String.class);
        func.invoke(p,"小军");
        System.out.println(p.GetName());
    }
}

class DemoReflectTest{
    public  void Test() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Properties pro=new Properties();
        ClassLoader class_loader=DemoReflectTest.class.getClassLoader();
        InputStream is=class_loader.getResourceAsStream("pro.properties");

        //FileInputStream is=new FileInputStream("src\\learn\\advance\\reflect1\\pro.properties");


         pro.load(is);

        String class_name=pro.getProperty("className");
        System.out.println(class_name);
        String method_name=pro.getProperty("methodName");

        Class cls=Class.forName(class_name);
        Object obj=cls.newInstance();
        Method method=cls.getMethod(method_name);

        method.invoke(obj);
    }

}

public class TestReflect {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException, IOException {
       // Demo1Reflect.Test();
       // DemoReflectTest.Test();
//        DemoReflectTest d=new DemoReflectTest();
//        d.Test();

        byte[] snun={1,2,4};
        System.out.println(Arrays.toString(snun));
        String content=Arrays.toString(snun);

        System.out.println("22221 "+content);

        byte[] message=content.getBytes(StandardCharsets.UTF_8);
        System.out.println(Arrays.toString(message));



    }
}
